package com.simpd.model;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import cn.pedant.SweetAlert.SweetAlertDialog;

import com.simpd.activity.ExampleActivity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.util.Log;

public class AssetHelperTask extends AsyncTask<Void, Void, Void> {
	private ProgressDialog dialog;
	private Context context;
	public AssetHelperTask(Context context, ProgressDialog dialog) {
		this.dialog 	= dialog;
		this.context	= context;
	}
	
	@Override
	protected void onPreExecute() {
		this.dialog.show();
	}
	
	@Override
	protected Void doInBackground(Void... params) {
		putAssetFiles();
		return null;
	}
	
	@Override
	protected void onPostExecute(Void result) {
		this.dialog.dismiss();
	}
	
	private void putAssetFiles(){
		ArrayList<String> targetDirPaths = new ArrayList<String>(); 
		targetDirPaths.add(SynchronizeConstant.SIMPD_DIR_PATH);
		targetDirPaths.add(SynchronizeConstant.CSS_DIR_PATH);
		targetDirPaths.add(SynchronizeConstant.JS_DIR_PATH); 
		targetDirPaths.add(SynchronizeConstant.JSIGNATURE_DIR_PATH); 
		targetDirPaths.add(SynchronizeConstant.TELAAH_DIR_PATH);
		targetDirPaths.add(SynchronizeConstant.SPT_DIR_PATH);
		boolean isTargetCreated = false; 
		
		for (String dirPath : targetDirPaths) {
			isTargetCreated = new File(dirPath).mkdir();
			Log.d("[create-dir]", "create dir "+dirPath+" successfully ? : "+isTargetCreated);
		}
		
		AssetManager assetManager 	= this.context.getAssets(); 
		String[] assetNames 		= null; 
		
		try {
			assetNames = assetManager.list("");
			String assetNamesToString	= null;
			
			for (int i = 0; i < assetNames.length; i++) {
				assetNamesToString+= " "+assetNames[i]+", ";
			}
			Log.d("assets-files", "asset files : "+assetNamesToString);
			
			File file = null;
			InputStream in 		= null; 
			OutputStream out 	= null; 
			
			for (String assetFile : assetNames) {
				try {
					
					if(assetFile.equalsIgnoreCase(SynchronizeConstant.JQUERY_JS) 
					   || assetFile.equalsIgnoreCase(SynchronizeConstant.JSIGNATURE_JS)){
						file = new File(SynchronizeConstant.JSIGNATURE_DIR_PATH, assetFile);
					}else if(assetFile.equalsIgnoreCase(SynchronizeConstant.JQUERY_MOBILE_JS)){
						file = new File(SynchronizeConstant.JS_DIR_PATH, assetFile);
					}else if(assetFile.equalsIgnoreCase(SynchronizeConstant.JQUERY_MOBILE_CSS)){
						file = new File(SynchronizeConstant.CSS_DIR_PATH, assetFile);
					}
					
					if(file != null && !file.exists()){
						in 	= assetManager.open(assetFile);
						out = new FileOutputStream(file);
						copyFile(in, out);
						in.close();
						in 	= null;
						out.flush(); 
						out.close();
						out = null;
						Log.d("[copying-data]", "successfully copying data..!");
					}
				} catch (Exception e) {
					Log.e("[error-Exception]", e.getMessage());
					e.printStackTrace();
				}
				
			}
			
			
			
		} catch (IOException e) {
			Log.e("[error-IOException]", e.getMessage());
			e.printStackTrace();
		}
	}
	
	private void copyFile(InputStream in, OutputStream out){
		byte[] buffer = new byte[1024];
		int read ; 
		try {
			while((read=in.read(buffer)) != -1){
				out.write(buffer, 0, read);
			}
		} catch (IOException e) {
			Log.e("[error - copyFile]", "copying file error cause : "+e.getMessage());
			e.printStackTrace();
		}
	}

}
